package com.macro.mall.product.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.PmProdSpec;
import com.macro.mall.product.dto.PmProdSpecDto;
import com.macro.mall.product.dto.PmProdSpecSaveDto;
import com.macro.mall.product.service.PmProdSpecService;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;
import java.util.List;

/**
 * 属性管理Controller
 * Created by macro on 2018/4/26.
 */
@Controller
@Api(tags = "PmProdSpecController", description = "商品规格管理")
@RequestMapping("/prodSpec")
public class PmProdSpecController {

    @Autowired
    private PmProdSpecService pmProdSpecService;

    @Autowired
    private Mapper mapper;

    @ApiOperation(value = "服务规格列表--不分页")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmProdSpecDto>> list(@RequestParam(value = "prodSpecName", defaultValue = "") String prodSpecName,
                                                    @RequestParam(value = "serverSpecId", defaultValue = "") Long serverSpecId
    ) {
        PmProdSpecDto prodSpecDto = new PmProdSpecDto();
        if(!StringUtils.isBlank(prodSpecName)){
            prodSpecDto.setSpecName(prodSpecName);
        }
        if(serverSpecId != null){
            prodSpecDto.setServerSpecId(serverSpecId);
        }
        List<PmProdSpec> prodSpecList = pmProdSpecService.list(prodSpecDto);
        List<PmProdSpecDto> dtoList  = pmProdSpecService.converDataList(prodSpecList);
        return CommonResult.success(dtoList);
    }
    @ApiOperation(value = "服务规格列表--分页")
    @RequestMapping(value = "/listPage", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<PmProdSpecDto>> listPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                            @RequestParam(value = "prodSpecName", defaultValue = "") String prodSpecName,
                                                            @RequestParam(value = "serverSpecId", defaultValue = "") Long serverSpecId
    ) {
        PmProdSpecDto prodSpecDto = new PmProdSpecDto();
        if(!StringUtils.isBlank(prodSpecName)){
            prodSpecName= URLDecoder.decode(prodSpecName);
            prodSpecDto.setSpecName(prodSpecName);
        }
        if(serverSpecId != null){
            prodSpecDto.setServerSpecId(serverSpecId);
        }
        List<PmProdSpec> prodSpecList = pmProdSpecService.listPage(pageNum,pageSize,prodSpecDto);
        PageInfo<PmProdSpec> pageInfo = new PageInfo<PmProdSpec>(prodSpecList);

        List<PmProdSpecDto> dtoList  = pmProdSpecService.converDataList(prodSpecList);

        CommonPage<PmProdSpecDto> result = new CommonPage<PmProdSpecDto>();
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotal(pageInfo.getTotal());
        result.setTotalPage(pageInfo.getPages());
        result.setList(dtoList);

        return CommonResult.success(result);
    }

    @ApiOperation(value = "保存产品规格")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult save(@RequestBody PmProdSpecSaveDto pmProdSpecDto, BindingResult result) {
        CommonResult commonResult;
        int count = pmProdSpecService.save(pmProdSpecDto);
        if (count == 1) {
            commonResult = CommonResult.success(count);
        } else {
            commonResult = CommonResult.failed();
        }
        return commonResult;
    }
    @ApiOperation(value = "删除产品规格")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult delete(@RequestParam(value = "prodSpecIds") String prodSpecIds) {
        CommonResult commonResult;
        int count = pmProdSpecService.delete(prodSpecIds);
        if (count > 0) {
            commonResult = CommonResult.success(count);
        } else {
            commonResult = CommonResult.failed();
        }
        return commonResult;
    }
    @ApiOperation(value = "获取单个产品规格")
    @RequestMapping(value = "/getProdSpecById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmProdSpecDto> getProdSpecById(@RequestParam(value = "prodSpecId", defaultValue = "") Long prodSpecId
    ) {
        PmProdSpec prodSpec = pmProdSpecService.getById(prodSpecId);
        PmProdSpecDto prodSpecDto= pmProdSpecService.converDataInfo(prodSpec);
        return CommonResult.success(prodSpecDto);
    }
    @ApiOperation(value = "根据属性值获取价格列表")
    @RequestMapping(value = "/getProdSpecSkuListShow", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<JSONObject> getProdSpecSkuListShow(@RequestParam(value = "prodSpecId", defaultValue = "") Long prodSpecId,
                                                    @RequestParam(value = "packageId", defaultValue = "") Long packageId
    ) {

        JSONObject retObj = pmProdSpecService.getProdSpecSkuListShow(prodSpecId,packageId);
       return CommonResult.success(retObj);
    }
}
